package com.devshephard.juchm.reentrantlock;
import java.util.concurrent.locks.ReentrantLock;

import static java.lang.Thread.sleep;

public class ReentrantLockInterruptDemo {

    public static void main(String[] args) throws InterruptedException {

        ReentrantLock lock = new ReentrantLock();

        Thread t1 = new Thread(()->{
            System.out.println("启动...");
            try {
                lock.lockInterruptibly();  //如果希望锁被打断,这里应该是使用lockInterruptibly()方法
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println("等锁的过程中被打断");
                return;
            }
            try {
                System.out.println("获得了锁");
            } finally {
                lock.unlock();
            }
        },"A");


        lock.lock();
        System.out.println("获得了锁");
        t1.start();

        try {
            sleep(1);
            t1.interrupt();
            System.out.println("执行打断");
        } finally {
            lock.unlock();
        }

    }
}
